#include "ModRunFlyweight.h"
#include <QDebug>

#include "../Output/FactoryOutput.h"

#include "Gof23/Gof_Flyweight/FlyFactoryStatic.h"
#include "Gof23/Gof_Flyweight/FlyComponent.h"

//享元模式（Flyweight）;
ModRunFlyweight::ModRunFlyweight()
{
    //模式名称;
    m_strModelName = "享元模式（Flyweight）;";
    //模式描述;
    m_strModelDesc = "享元模式：运用共享技术有效地支持大量细粒度的对象。\n"
        "在有大量对象时，把其中共同的部分抽象出来，如果有相同的业务请求，直接返回内存中已有的对象，避免重新创建。\n\n"
        "以下情况可以考虑使用享元模式：\n"
        "系统中有大量的对象，这些对象消耗大量的内存，且这些对象的状态可以被外部化。\n"
                     ;
}

//初始化显示模式;
void ModRunFlyweight::InitDisplay()
{
    qDebug() << m_strModelName;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateDescOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ m_strModelName,m_strModelDesc};
        pOutPut->OutPut(outList);
    }
}

//返回模式类型;
EnumModRun ModRunFlyweight::GetEnum()
{
    return emModRunFlyweight;
}

//运行模式;
void ModRunFlyweight::ModRun()
{
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outListStart{ "========享元模式 start:=======" };
        std::initializer_list<QString> outListEnd{ "========享元模式 end:=======" };
        pOutPut->OutPut(outListStart);

        //享元模式;
        std::shared_ptr<FlyComponent> pFly00 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly00");
        std::shared_ptr<FlyComponent> pFly01 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly01");
        std::shared_ptr<FlyComponent> pFly02 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly02");
        std::shared_ptr<FlyComponent> pFly03 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly03");

        FlyFactoryStatic::GetInstance()->Display();

        std::shared_ptr<FlyComponent> pFly04 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly00");
        std::shared_ptr<FlyComponent> pFly05 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly01");
        std::shared_ptr<FlyComponent> pFly06 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly02");
        std::shared_ptr<FlyComponent> pFly07 = FlyFactoryStatic::GetInstance()->GetFlyCom("Fly03");

        FlyFactoryStatic::GetInstance()->Display();

        pOutPut->OutPut(outListEnd);
    }

}

InterFaceModRun* ModRunFlyweight::CreateMe()
{
    return new ModRunFlyweight();
}
